package com.bustle.commons.data.dto;

import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;

import com.bustle.commons.CollectionManager;
import com.bustle.commons.MapConverter;
import com.bustle.commons.data.dao.Dao;

abstract public class DtoDao<KeyType, DtoType, Type> implements Dao<KeyType, DtoType>
{
  abstract protected Dao<KeyType, Type> getDao();
  abstract protected MapConverter<DtoType, Type> getDtoToDataMapConverter();
  abstract protected MapConverter<Type, DtoType> getDataToDtoMapConverter();

  @Override
  public void create(DtoType aDto)
  {
    Type data = getDtoToDataMapConverter().convert(aDto);
    getDao().create(data);
    getDataToDtoMapConverter().map(data, aDto);
  }

  @Override
  public DtoType retrieve(KeyType aKey)
  {
    return getDataToDtoMapConverter().convert(getDao().retrieve(aKey));
  }

  @Override
  public List<DtoType> retrieveAll()
  {
    List<DtoType> daos = new Vector<>();
    collectionManager.addAll(getDao().retrieveAll(), daos, getDataToDtoMapConverter());
    return daos;
  }

  @Override
  public void update(DtoType aDto)
  {
    getDao().update(getDtoToDataMapConverter().convert(aDto));
  }

  @Override
  public void delete(KeyType aKey)
  {
    getDao().delete(aKey);
  }

  @EJB
  protected CollectionManager collectionManager;
}
